package development;

import java.util.Vector;

import state.util.Hashtable;
import development.bean.SMSServiceCommand;
import development.bean.Subscription;

public class Utils {
  public static String[] split(String original, String separator) {
    Vector nodes = new Vector();
    int index = original.indexOf(separator);
    while (index >= 0) {
      nodes.addElement(original.substring(0, index));
      original = original.substring(index + separator.length());
      index = original.indexOf(separator);
    }
    nodes.addElement(original);
    String[] result = new String[nodes.size()];
    if (nodes.size() > 0)
      for (int loop = 0; loop < nodes.size(); loop++)
        result[loop] = (String) nodes.elementAt(loop);
    return result;
  }

  public static Vector getCurrentSubscriptions(String serviceCode) {
    Hashtable subs = (Hashtable) Global.subscriptionMap.get(Global.currentUser.getName());
    if (subs == null) {
      return null;
    }
    return (Vector) subs.get(serviceCode);
  }

  public static Subscription getSubcription(String serviceCode, String command) {
    Vector subService = getCurrentSubscriptions(serviceCode);
    if (subService == null || subService.isEmpty()) {
      return null;
    }
    for (int i = 0; i < subService.size(); i++) {
      Subscription sub = (Subscription) subService.elementAt(i);
      if (sub.request.equals(command)) {
        return sub;
      }
    }
    return null;
  }

  public static boolean isSubscriber(String serviceCode, String command) {
    return getSubcription(serviceCode, command) != null;
  }

  public static SMSServiceCommand getSMSServiceCommand(String serviceCode, String command) {
    Hashtable smsServiceCommandMap = (Hashtable) Global.smsServiceCommand.get(String.valueOf(Global.currentUser.getProviderId()));
    if (smsServiceCommandMap == null) {
      return null;
    }
    Vector serviceCommandList = (Vector) smsServiceCommandMap.get(serviceCode);
    if (serviceCommandList == null || serviceCommandList.isEmpty()) {
      return null;
    }
    for (int i = 0; i < serviceCommandList.size(); i++) {
      SMSServiceCommand sms = (SMSServiceCommand) serviceCommandList.elementAt(i);
      if (sms != null && sms.command.equals(command)) {
        return sms;
      }
    }
    return null;
  }  
}
